package com.central.apps.controller;

import cn.hutool.json.JSONUtil;
import com.central.app.service.HandlerService;
import com.central.bizcommon.annotation.log.BussinessLog;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

@RestController
@Api(tags = "app的自定义api")
@RequestMapping("/QRCode")
@Slf4j
public class QRCodeUtil {

    @Autowired private HandlerService handlerService;

//    @PostMapping("/insertStoreItem")
//    public Map<String,Object> insertStoreItem(@RequestBody Map<String,Object> params){
//        Integer page = MapUtils.getInteger(params,"page");
//        Integer limit = MapUtils.getInteger(params,"limit");
//        page =(page -1)*limit;
//        String pageLimit =" limit "+page+","+limit;
//        HashMap<String, Object> result = new HashMap<>();
//
//        List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_item where status = '活动'" + pageLimit);
//        result.put("code",0);
//        result.put("count",bySql.size());
//        result.put("data",bySql);
//        return result;
//    }

    @PostMapping("/getItem")
    public Map<String,Object> getItem(@RequestBody Map<String,Object> params){
        Map<String,Object> result =new HashMap<>();
        Integer page = MapUtils.getInteger(params,"page");
        Integer limit = MapUtils.getInteger(params,"limit");
        page =(page -1)*limit;
        String pageLimit =" limit "+page+","+limit;
        String sql1="select * from gq_item where itemnum in (select itemnum from gq_inventory where storenum in (select storenum  from gq_store where usestatus ='使用中'))";
        String sql ="";
        List<Map<String, Object>> bySql = handlerService.findBySql(sql1);
        ArrayList<String> objects = new ArrayList<>();
        String itemnum = MapUtils.getString(params, "itemnum");
        String description = MapUtils.getString(params, "description");
        String maxnum = MapUtils.getString(params, "maxnum");
        String mediumnum = MapUtils.getString(params, "mediumnum");
        String smallnum = MapUtils.getString(params, "smallnum");
        objects.add(itemnum);
        objects.add("itemnum");
        objects.add(description);
        objects.add("description");
        objects.add(maxnum);
        objects.add("maxnum");
        objects.add(mediumnum);
        objects.add("mediumnum");
        objects.add(smallnum);
        objects.add("smallnum");
        if (itemnum==null&&description==null&&maxnum==null&&mediumnum==null&&smallnum==null){
            sql="select * from gq_item t where t.itemnum in (select itemnum from gq_inventory where storenum in (select storenum  from gq_store where usestatus ='使用中'))";
            sql =sql + pageLimit;
        }else {

            String list = "t.";
            String list1=" like";
            String list3="";
            for (int i=0;i<objects.size();i=i+2){
                String s = objects.get(i);
                if (s!=null){
                    if(list3==""){
                        list3=list3+list+objects.get(i+1)+list1+"'%"+s+"%'";
                }else {
                        list3=list3+" and "+list+objects.get(i+1)+list1+"'%"+s+"%'";
                    }
                }
            }
            sql="select * from gq_item t where t.itemnum in (select itemnum from gq_inventory where storenum in (select storenum  from gq_store where usestatus ='使用中')) and "+list3+"";
            sql =sql +pageLimit;
        }


        List<Map<String, Object>> bySql1 = handlerService.findBySql(sql);
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String reqdeliverydate =sdf.format(new Date());
        for(Map<String, Object> map :bySql1){
            map.put("reqdeliverydate",reqdeliverydate);
        }
        result.put("code",0);
        result.put("count",bySql.size());
        result.put("data",bySql1);
        return result;
    }

    @PostMapping("/getItems")
    public Map<String,Object> getItems(@RequestBody Map<String,Object> params){
        Map<String,Object> result =new HashMap<>();
        String prnum = MapUtils.getString(params,"prnum");
        String ponum = MapUtils.getString(params,"ponum");
        String receiptnum = MapUtils.getString(params,"receiptnum");
        String sql ="select a.itemnum,a.itemdesc,b.itemtype,b.manufacturer,b.model from gq_invbalances a left join gq_inventory b on a.itemnum=b.itemnum and a.storenum = b.storenum ";
        String condition ="";
        if(receiptnum!=""){
            condition =" where a.itemnum in (select c.itemnum from gq_prtdetailline c where receiptnum ='"+receiptnum+"')";
        }else if(ponum!=""){
            condition =" where a.itemnum in (select d.itemnum from gq_poline d where ponum ='"+ponum+"')";
        }else if(prnum!=""){
            condition =" where a.itemnum in (select e.itemnum from gq_ppline e where ppnum ='"+prnum+"')";
        }
        Integer page = MapUtils.getInteger(params,"page");
        Integer limit = MapUtils.getInteger(params,"limit");
        page =(page -1)*limit;
        String pageLimit =" limit "+page+","+limit;
        sql =sql +condition +pageLimit;
        List<Map<String, Object>> bySql = handlerService.findBySql(sql);
        result.put("code",0);
        result.put("count",bySql.size());
        result.put("data",bySql);
        for(Map<String, Object> map:bySql){
            String itemnum =MapUtils.getString(map,"itemnum");
            List<Map<String, Object>> bySql1 = handlerService.findBySql("select batchnum from gq_invbalances where itemnum ='" + itemnum + "' order by batchnum desc limit 1");
            SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
            String now =sdf.format(new Date())+"0001";
            Long batchnum =Long.parseLong(now);
            Long num =MapUtils.getLong(bySql1.get(0),"batchnum");
            if(num !=null){
                batchnum =num;
                batchnum++;
            }
            log.info("batchnum++++++++++++++++++++++++++++++++++++++++++"+batchnum);
            map.put("batchnum",Long.toString(batchnum));
        }
        return result;
    }

    /**
     * 出库申请选择物料按钮
     * @param
     * @return
     */
    @PostMapping("/getitemapplys")
    public Map<String,Object> getitemapplys(@RequestBody Map<String,Object> params){
        Map<String,Object> result =new HashMap<>();
        String storenum =MapUtils.getString(params,"storenum");
        String proid = MapUtils.getString(params,"proid");
        String taskid = MapUtils.getString(params,"taskid");
        String itemapplynum = MapUtils.getString(params, "glnum");
        String itemnum1 = MapUtils.getString(params, "itemnum");
        String itemdesc = MapUtils.getString(params, "itemdesc");//物料名称
        String model1 = MapUtils.getString(params, "model");//型号
        String maxnum = MapUtils.getString(params, "maxnum");
        String mediumnum = MapUtils.getString(params, "mediumnum");
        String smallnum = MapUtils.getString(params, "smallnum");
        String applicantid = MapUtils.getString(params, "applicantid");
        String subjectno = MapUtils.getString(params, "subjectno");
        ArrayList<String> strings = new ArrayList<>();
        strings.add(itemnum1);
        strings.add("itemnum");
        strings.add(maxnum);
        strings.add("maxnum");
        strings.add(mediumnum);
        strings.add("mediumnum");
        strings.add(smallnum);
        strings.add("smallnum");
        List<Map<String, Object>> bySql=null;
        List<Map<String,Object>> bySql3=null;
        String sql1="";
        List<Map<String, Object>> bySql2 = handlerService.findBySql("select subjectno from gq_itemapplyline where glnum='" + itemapplynum + "'");
        List<Map<String, Object>> bySql4 = handlerService.findBySql("SELECT * from biz_attribute where object_id ='176'");
        String columnname="";
        String string="";

        if (CollectionUtils.isNotEmpty(bySql4)){
            for (int i=0;i<bySql4.size();i++){
                Map<String, Object> map = bySql4.get(i);
                columnname = MapUtils.getString(map, "columnname");
                //字段名_orders: "asc"
                String columnname1= columnname+"_orders";
                String columnname11 = MapUtils.getString(params, columnname1);

                if (StringUtils.isNotEmpty(columnname11)) {
                    string  = "order by "+columnname+" "+columnname11;
                }else{
                    //自动拣货 库管员可以设置排序规则，根据配置的规则进行排序
                    string = getCollation();
                }
            }
        }
        //判断是否存在子表
        if (CollectionUtils.isNotEmpty(bySql2)){
            Map<String, Object> map1 = bySql2.get(0);
            String subjectno1 = MapUtils.getString(map1, "subjectno");
            List<Map<String, Object>> sys_setting = handlerService.findBySql("select field8 from sys_setting");//系统设置控制开关
            String sql="";
            if (CollectionUtils.isNotEmpty(sys_setting)) {
                String field8 = MapUtils.getString(sys_setting.get(0), "field8");
                if (field8.equals("1")) {
                    sql   ="select c.proname,a.*,b.maxnum,b.mediumnum,b.smallnum,b.itemnum,b.materialtype,b.specifications,b.id as itemid,current_date as collectime from gq_invbalances a left join gq_item b on a.itemnum=b.itemnum LEFT JOIN gq_proaccount c on a.proid=c.id where  (a.id in (select invbalancesid from gq_occurecords where applyby='"+applicantid+"')or a.avblbalance >0) and a.storenum ='"+storenum+"'";

                }else {
                    sql   ="select c.proname,a.*,b.maxnum,b.mediumnum,b.smallnum,b.itemnum,b.materialtype,b.specifications,b.id as itemid,current_date as collectime from gq_invbalances a left join gq_item b on a.itemnum=b.itemnum LEFT JOIN gq_proaccount c on a.proid=c.id where  ( a.applyby IS NULL OR a.applyby = '"+applicantid+"' ) and a.storenum ='"+storenum+"' and a.avblbalance >0  ";

                }
            }
            String condition ="";
            String condition2 ="";
            String subjectnosql="";
            String itemdescsql="";
            String model1sql="";

            if (proid != null && proid != "") {
                condition = " and a.proid  ='" + proid + "'";
            }
            if (taskid != null && taskid != "") {
                condition2 = " and a.taskid ='" + taskid + "'";
            }
            if(StringUtils.isNotBlank(subjectno)){
                subjectnosql = " and a.subjectno = '" + subjectno1 + "'";
            }
            if(StringUtils.isNotBlank(itemdesc)){
                itemdescsql = " and a.itemdesc like '%"+itemdesc+"%'";
            }
            if(StringUtils.isNotBlank(model1)){
                model1sql = " and b.model like '%"+model1+"%'";
            }
            if (itemnum1==null&&maxnum==null&&mediumnum==null&&smallnum==null){
                String invbalanid = " and a.id not in (select c.invbalanid from gq_itemapplyline c where c.glnum ='"+itemapplynum+"')";
                Integer page = MapUtils.getInteger(params,"page");
                Integer limit = MapUtils.getInteger(params,"limit");
                page =(page -1)*limit;
                String pageLimit =" limit "+page+","+limit;
                sql1= sql +subjectnosql+condition+condition2+itemdescsql+model1sql+ invbalanid +string;
                sql =sql +subjectnosql+condition+condition2+itemdescsql+model1sql+ invbalanid +string+ pageLimit;
                bySql3=handlerService.findBySql(sql1);
                bySql = handlerService.findBySql(sql);
                if (CollectionUtils.isNotEmpty(bySql)){
                    for (int i=0;i<bySql.size();i++){
                        Map<String, Object> map = bySql.get(i);
                        String itemnum = MapUtils.getString(map,"itemnum");
                        List<Map<String, Object>> bySql1 = handlerService.findBySql("select unit,model from gq_item where itemnum='" + itemnum + "'");
                        if (CollectionUtils.isNotEmpty(bySql1)){
                            String unit = MapUtils.getString(bySql1.get(0), "unit");
                            String model = MapUtils.getString(bySql1.get(0), "model");
                            map.put("Orunit",unit);
                            map.put("model",model);
                        }


                    }
                }
            }else {
                String invbalanid = " and a.id not in (select c.invbalanid from gq_itemapplyline c where c.glnum ='"+itemapplynum+"')";
                Integer page = MapUtils.getInteger(params,"page");
                Integer limit = MapUtils.getInteger(params,"limit");
                page =(page -1)*limit;
                String pageLimit =" limit "+page+","+limit;
                String list = " b.";
                String list1=" like";
                String list3="";
                for (int i=0;i<strings.size();i=i+2){
                    String s = strings.get(i);
                    if (s!=null){
                        list3=list3+" and "+ list+strings.get(i+1)+list1+"'%"+s+"%'";
                    }
                }
                sql1= sql  +subjectnosql+condition +condition2+itemdescsql+model1sql+ invbalanid+list3+string;
                sql =sql  +subjectnosql+condition +condition2+itemdescsql+model1sql+ invbalanid+list3+string+pageLimit;
                bySql3=handlerService.findBySql(sql1);
                bySql = handlerService.findBySql(sql);
                if (CollectionUtils.isNotEmpty(bySql)){
                    for (int i=0;i<bySql.size();i++){
                        Map<String, Object> map = bySql.get(i);
                        String itemnum = MapUtils.getString(map,"itemnum");
                        List<Map<String, Object>> bySql1 = handlerService.findBySql("select unit,model from gq_item where itemnum='" + itemnum + "'");
                        if (CollectionUtils.isNotEmpty(bySql1)){
                            String unit = MapUtils.getString(bySql1.get(0), "unit");
                            String model = MapUtils.getString(bySql1.get(0), "model");
                            map.put("Orunit",unit);
                            map.put("model",model);
                        }


                    }
                }
            }
        }else {
            String sql ="select c.proname,a.*,b.maxnum,b.mediumnum,b.smallnum,b.itemnum,b.specifications,b.id as itemid,current_date as collectime from gq_invbalances a left join gq_item b on a.itemnum=b.itemnum LEFT JOIN gq_proaccount c on a.proid=c.id where  ( a.applyby IS NULL OR a.applyby = '"+applicantid+"' ) and a.storenum ='"+storenum+"' and a.avblbalance >0  ";
            String condition ="";
            String condition2 ="";
            String subjectnosql="";
            String itemdescsql="";
            String model1sql="";

            if (proid != null && proid != "") {
                condition = " and a.proid  ='" + proid + "'";
            }
            if (taskid != null && taskid != "") {
                condition2 = " and a.taskid ='" + taskid + "'";
            }
            if (StringUtils.isNotBlank(subjectno)) {
                subjectnosql = " and a.subjectno = '" + subjectno + "'";
            }
            if(StringUtils.isNotBlank(itemdesc)){
                itemdescsql = " and a.itemdesc like '%"+itemdesc+"%'";
            }
            if(StringUtils.isNotBlank(model1)){
                model1sql = " and b.model like '%"+model1+"%'";
            }
            if (itemnum1==null&&maxnum==null&&mediumnum==null&&smallnum==null){
                String invbalanid = " and a.id not in (select c.invbalanid from gq_itemapplyline c where c.glnum ='"+itemapplynum+"')";
                Integer page = MapUtils.getInteger(params,"page");
                Integer limit = MapUtils.getInteger(params,"limit");
                page =(page -1)*limit;
                String pageLimit =" limit "+page+","+limit;
                sql1= sql +subjectnosql+condition+condition2+itemdescsql+model1sql+ invbalanid +string;
                sql =sql +subjectnosql+condition+condition2+itemdescsql+model1sql+ invbalanid +string+ pageLimit;
                bySql3=handlerService.findBySql(sql1);
                bySql = handlerService.findBySql(sql);
                if (CollectionUtils.isNotEmpty(bySql)){
                    for (int i=0;i<bySql.size();i++){
                        Map<String, Object> map = bySql.get(i);
                        String itemnum = MapUtils.getString(map,"itemnum");
                        List<Map<String, Object>> bySql1 = handlerService.findBySql("select unit,model from gq_item where itemnum='" + itemnum + "'");
                        if (CollectionUtils.isNotEmpty(bySql1)){
                            String unit = MapUtils.getString(bySql1.get(0), "unit");
                            String model = MapUtils.getString(bySql1.get(0), "model");
                            map.put("Orunit",unit);
                            map.put("model",model);
                        }


                    }
                }
            }else {
                String invbalanid = " and a.id not in (select c.invbalanid from gq_itemapplyline c where c.glnum ='"+itemapplynum+"')";
                Integer page = MapUtils.getInteger(params,"page");
                Integer limit = MapUtils.getInteger(params,"limit");
                page =(page -1)*limit;
                String pageLimit =" limit "+page+","+limit;
                String list = " b.";
                String list1=" like";
                String list3="";
                for (int i=0;i<strings.size();i=i+2){
                    String s = strings.get(i);
                    if (s!=null){
                        list3=list3+" and "+ list+strings.get(i+1)+list1+"'%"+s+"%'";
                    }
                }
                sql1= sql  +subjectnosql+condition +condition2+itemdescsql+model1sql+ invbalanid+list3+string;
                sql =sql  +subjectnosql+condition +condition2+itemdescsql+model1sql+ invbalanid+list3+string+pageLimit;
                bySql3=handlerService.findBySql(sql1);
                bySql = handlerService.findBySql(sql);
                if (CollectionUtils.isNotEmpty(bySql)){
                    for (int i=0;i<bySql.size();i++){
                        Map<String, Object> map = bySql.get(i);
                        String itemnum = MapUtils.getString(map,"itemnum");
                        List<Map<String, Object>> bySql1 = handlerService.findBySql("select unit,model from gq_item where itemnum='" + itemnum + "'");
                        if (CollectionUtils.isNotEmpty(bySql1)){
                            String unit = MapUtils.getString(bySql1.get(0), "unit");
                            String model = MapUtils.getString(bySql1.get(0), "model");
                            map.put("Orunit",unit);
                            map.put("model",model);
                        }


                    }
                }
            }
        }


        result.put("code",0);
        result.put("count",bySql3.size());
        result.put("data",bySql);
        return result;
    }

    /**
     * 出库申请选择物料按钮批次
     * @param
     * @return
     */
    @PostMapping("/getitemapplyslotnum")
    public Map<String,Object> getitemapplyslotnum(@RequestBody Map<String,Object> params){
        Map<String,Object> result =new HashMap<>();
        String storenum =MapUtils.getString(params,"storenum");
        String proid = MapUtils.getString(params,"proid");
        String taskid = MapUtils.getString(params,"taskid");
        String itemapplynum = MapUtils.getString(params, "glnum");
        String itemnum1 = MapUtils.getString(params, "itemnum");
        String maxnum = MapUtils.getString(params, "maxnum");
        String mediumnum = MapUtils.getString(params, "mediumnum");
        String smallnum = MapUtils.getString(params, "smallnum");
        String applicantid = MapUtils.getString(params, "applicantid");
        String subjectno = MapUtils.getString(params, "subjectno");
        String itemnumss = MapUtils.getString(params, "itemnumss");
        ArrayList<String> strings = new ArrayList<>();
        strings.add(itemnum1);
        strings.add("itemnum");
        strings.add(maxnum);
        strings.add("maxnum");
        strings.add(mediumnum);
        strings.add("mediumnum");
        strings.add(smallnum);
        strings.add("smallnum");
        List<Map<String, Object>> bySql=null;
        List<Map<String,Object>> bySql3=null;
        String sql1="";
        List<Map<String, Object>> bySql2 = handlerService.findBySql("select subjectno from gq_itemapplyline where glnum='" + itemapplynum + "'");
        List<Map<String, Object>> bySql4 = handlerService.findBySql("SELECT * from biz_attribute where object_id ='176'");
        String columnname="";
        String string="";

        if (CollectionUtils.isNotEmpty(bySql4)){
            for (int i=0;i<bySql4.size();i++){
                Map<String, Object> map = bySql4.get(i);
                columnname = MapUtils.getString(map, "columnname");
                //字段名_orders: "asc"
                String columnname1= columnname+"_orders";
                String columnname11 = MapUtils.getString(params, columnname1);

                if (StringUtils.isNotEmpty(columnname11)) {
                    string  = "order by "+columnname+" "+columnname11;
                }else{
                    //自动拣货 库管员可以设置排序规则，根据配置的规则进行排序
                    string = getCollation();
                }
            }
        }
        //判断是否存在子表
        if (CollectionUtils.isNotEmpty(bySql2)){
            Map<String, Object> map1 = bySql2.get(0);
            String subjectno1 = MapUtils.getString(map1, "subjectno");
            String sql ="select c.proname,a.*,b.maxnum,b.mediumnum,b.smallnum,b.itemnum,b.materialtype,b.specifications,b.id as itemid,current_date as collectime from gq_invbalances a left join gq_item b on a.itemnum=b.itemnum LEFT JOIN gq_proaccount c on a.proid=c.id where  ( a.applyby IS NULL OR a.applyby = '"+applicantid+"' ) and a.storenum ='"+storenum+"' and a.avblbalance >0  and a.itemnum='"+itemnumss+"'";
            String condition ="";
            String condition2 ="";
            String subjectnosql="";

//            if(proid!=null&&proid!=""){
//                condition =" and a.proid  ='"+proid+"'";
//            }
//        if(taskid!=null&&taskid!=""){
//            condition2 =" and a.taskid ='"+taskid+"'";
//        }
            if(StringUtils.isNotBlank(subjectno)){
                subjectnosql = " and a.subjectno = '" + subjectno1 + "'";
            }
            if (itemnum1==null&&maxnum==null&&mediumnum==null&&smallnum==null){
                String invbalanid = " and a.id not in (select c.invbalanid from gq_itemapplyline c where c.glnum ='"+itemapplynum+"')";
                Integer page = MapUtils.getInteger(params,"page");
                Integer limit = MapUtils.getInteger(params,"limit");
                page =(page -1)*limit;
                String pageLimit =" limit "+page+","+limit;
                sql1= sql +subjectnosql+condition+ invbalanid;
                sql =sql +subjectnosql+condition+ invbalanid +string+ pageLimit;
                bySql3=handlerService.findBySql(sql1);
                bySql = handlerService.findBySql(sql);
                if (CollectionUtils.isNotEmpty(bySql)){
                    for (int i=0;i<bySql.size();i++){
                        Map<String, Object> map = bySql.get(i);
                        String itemnum = MapUtils.getString(map,"itemnum");
                        List<Map<String, Object>> bySql1 = handlerService.findBySql("select unit,model from gq_item where itemnum='" + itemnum + "'");
                        if (CollectionUtils.isNotEmpty(bySql1)){
                            String unit = MapUtils.getString(bySql1.get(0), "unit");
                            String model = MapUtils.getString(bySql1.get(0), "model");
                            map.put("Orunit",unit);
                            map.put("model",model);
                        }


                    }
                }
            }else {
                String invbalanid = " and a.id not in (select c.invbalanid from gq_itemapplyline c where c.glnum ='"+itemapplynum+"')";
                Integer page = MapUtils.getInteger(params,"page");
                Integer limit = MapUtils.getInteger(params,"limit");
                page =(page -1)*limit;
                String pageLimit =" limit "+page+","+limit;
                String list = " b.";
                String list1=" like";
                String list3="";
                for (int i=0;i<strings.size();i=i+2){
                    String s = strings.get(i);
                    if (s!=null){
                        list3=list3+" and "+ list+strings.get(i+1)+list1+"'%"+s+"%'";
                    }
                }
                sql1= sql +subjectnosql+condition+ invbalanid;
                sql =sql  +subjectnosql+condition + invbalanid+list3+string+pageLimit;
                bySql3=handlerService.findBySql(sql1);
                bySql = handlerService.findBySql(sql);
                if (CollectionUtils.isNotEmpty(bySql)){
                    for (int i=0;i<bySql.size();i++){
                        Map<String, Object> map = bySql.get(i);
                        String itemnum = MapUtils.getString(map,"itemnum");
                        List<Map<String, Object>> bySql1 = handlerService.findBySql("select unit,model from gq_item where itemnum='" + itemnum + "'");
                        if (CollectionUtils.isNotEmpty(bySql1)){
                            String unit = MapUtils.getString(bySql1.get(0), "unit");
                            String model = MapUtils.getString(bySql1.get(0), "model");
                            map.put("Orunit",unit);
                            map.put("model",model);
                        }


                    }
                }
            }
        }else {
            String sql ="select c.proname,a.*,b.maxnum,b.mediumnum,b.smallnum,b.itemnum,b.specifications,b.id as itemid,current_date as collectime from gq_invbalances a left join gq_item b on a.itemnum=b.itemnum LEFT JOIN gq_proaccount c on a.proid=c.id where  ( a.applyby IS NULL OR a.applyby = '"+applicantid+"' ) and a.storenum ='"+storenum+"' and a.avblbalance >0  ";
            String condition ="";
            String condition2 ="";
            String subjectnosql="";

            if(proid!=null&&proid!=""){
                condition =" and a.proid  ='"+proid+"'";
            }
//        if(taskid!=null&&taskid!=""){
//            condition2 =" and a.taskid ='"+taskid+"'";
//        }
            if(StringUtils.isNotBlank(subjectno)){
                subjectnosql = " and a.subjectno = '" + subjectno + "'";
            }
            if (itemnum1==null&&maxnum==null&&mediumnum==null&&smallnum==null){
                String invbalanid = " and a.id not in (select c.invbalanid from gq_itemapplyline c where c.glnum ='"+itemapplynum+"')";
                Integer page = MapUtils.getInteger(params,"page");
                Integer limit = MapUtils.getInteger(params,"limit");
                page =(page -1)*limit;
                String pageLimit =" limit "+page+","+limit;
                sql1= sql +subjectnosql+condition+ invbalanid;
                sql =sql +subjectnosql+condition+ invbalanid +string+ pageLimit;
                bySql3=handlerService.findBySql(sql1);
                bySql = handlerService.findBySql(sql);
                if (CollectionUtils.isNotEmpty(bySql)){
                    for (int i=0;i<bySql.size();i++){
                        Map<String, Object> map = bySql.get(i);
                        String itemnum = MapUtils.getString(map,"itemnum");
                        List<Map<String, Object>> bySql1 = handlerService.findBySql("select unit,model from gq_item where itemnum='" + itemnum + "'");
                        if (CollectionUtils.isNotEmpty(bySql1)){
                            String unit = MapUtils.getString(bySql1.get(0), "unit");
                            String model = MapUtils.getString(bySql1.get(0), "model");
                            map.put("Orunit",unit);
                            map.put("model",model);
                        }


                    }
                }
            }else {
                String invbalanid = " and a.id not in (select c.invbalanid from gq_itemapplyline c where c.glnum ='"+itemapplynum+"')";
                Integer page = MapUtils.getInteger(params,"page");
                Integer limit = MapUtils.getInteger(params,"limit");
                page =(page -1)*limit;
                String pageLimit =" limit "+page+","+limit;
                String list = " b.";
                String list1=" like";
                String list3="";
                for (int i=0;i<strings.size();i=i+2){
                    String s = strings.get(i);
                    if (s!=null){
                        list3=list3+" and "+ list+strings.get(i+1)+list1+"'%"+s+"%'";
                    }
                }
                sql1= sql +subjectnosql+condition+ invbalanid;
                sql =sql  +subjectnosql+condition + invbalanid+list3+string+pageLimit;
                bySql3=handlerService.findBySql(sql1);
                bySql = handlerService.findBySql(sql);
                if (CollectionUtils.isNotEmpty(bySql)){
                    for (int i=0;i<bySql.size();i++){
                        Map<String, Object> map = bySql.get(i);
                        String itemnum = MapUtils.getString(map,"itemnum");
                        List<Map<String, Object>> bySql1 = handlerService.findBySql("select unit,model from gq_item where itemnum='" + itemnum + "'");
                        if (CollectionUtils.isNotEmpty(bySql1)){
                            String unit = MapUtils.getString(bySql1.get(0), "unit");
                            String model = MapUtils.getString(bySql1.get(0), "model");
                            map.put("Orunit",unit);
                            map.put("model",model);
                        }


                    }
                }
            }
        }


        result.put("code",0);
        result.put("count",bySql3.size());
        result.put("data",bySql);
        return result;
    }

    //自动拣货获取库管员配置的排序规则
    private String getCollation() {
        List<Map<String, Object>> bySql = handlerService.findBySql("SELECT a.* from gq_pickingdtlcfg a LEFT JOIN gq_pickingcfg b on a.pickingid=b.id ORDER BY a.priority");
        if(CollectionUtils.isEmpty(bySql)){
            return "";
        }

        StringBuilder stringBuilder = new StringBuilder(" order by ");
        StringJoiner stringJoiner = new StringJoiner(",");
        for (Map<String, Object> map : bySql) {

            String fieldname = MapUtils.getString(map, "fieldname");//字段名称
            String collation = MapUtils.getString(map, "collation");//排序规则

            String collationNew = "";
            switch (collation){
                case "升序":
                    collationNew = " asc";
                    break;
                case "倒序":
                    collationNew = " desc";
                    break;
            }
            String s = fieldname + " " + collationNew;
            stringJoiner.add(s);
        }
        return stringBuilder.append(stringJoiner.toString()).toString();
    }




    @GetMapping ("/getAssetByAssetnum")
    @BussinessLog(module = "apps-center", value = "根据资产编号获取对应资产信息")
    public Map<String, Object> getAssetByAssetnum(@RequestParam String assetnum){
        String sql ="select * from bl_asset where assetnum ='"+assetnum+"';";
        List<Map<String, Object>> bySql = handlerService.findBySql(sql);
        return bySql.get(0);
    }

    @PostMapping("/QRCodeUtil")
    @BussinessLog(module = "apps-center", value = "将资产编号生成二维码")
    public void  QRCodeUtil( HttpServletRequest request, HttpServletResponse response) {
        String sql ="select assetnum from bl_asset where id ="+request.getParameter("id");
        List<Map<String, Object>> bySql = handlerService.findBySql(sql);
        String assetnum = JSONUtil.toJsonStr(bySql.get(0).get("assetnum"));
        //设置String类型的文件路径
        String filePath="D:/java/baofeng.png";
        //宁夏宝丰集团有限公司/baofeng.png";
        //String filePath = "/home/ubuntu/cnooc/doclinks/QRcode/assetnum"+assetnum+".png";
        try {
            boolean needLogo = false;
            //如果有这个参数，并且参数的值为true
            int qrWidth = 240;
            int qrHeight = 240;
            //如果有w这个参数，并且参数的值为true
            String width = request.getParameter("width");
            if (width != null && !"".equals(width)) {
                try {
                    qrWidth = Integer.parseInt(width);
                }
                catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            qrHeight = qrWidth;
            //开始创建图片，传输参数，连接、文件路径、高度、宽度、logo图片
            BufferedImage image = createImage(assetnum, filePath, qrWidth, qrHeight, needLogo, true);
            File file =new File(File.separator+"QRcode"+File.separator+"assetnum"+assetnum+".png");
            if(!file.exists()){
                file.mkdirs();
            }
            ImageIO.write(image, "png", file);
            String imagePath =file.getPath();
            String insertSql ="update bl_asset set QRcode ='"+imagePath+"' where id ="+request.getParameter("id");
            handlerService.updateBySql(insertSql);
            //设置返回的接口的type为image  且为png格式
            response.setContentType("image/png");
            //io流写入图片
            ImageIO.write(image,"png",(OutputStream)response.getOutputStream());

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * @description 生成图片
     * @param content
     * @param imgPath
     * @param qrWidth
     * @param qrHeight
     * @param needLogo
     * @param needCompress
     * @return
     * @throws Exception
     */
    private static BufferedImage createImage(final String content, final String imgPath, final int qrWidth, final int qrHeight, final boolean needLogo, final boolean needCompress) throws Exception {
        final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 2);
        final BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrWidth, qrHeight, (Map)hints);
        final int width = bitMatrix.getWidth();
        final int height = bitMatrix.getHeight();
        final BufferedImage image = new BufferedImage(width, height, 1);

        //二维码的像素
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? -16777216: -1);
            }
        }
        if (imgPath == null || "".equals(imgPath)) {
            return image;
        }
        if (needLogo) {
            //这里写入img
            insertImage(image, imgPath, qrWidth, qrHeight, needCompress);
        }
        //最终返回一个图片出来BufferedImage
        return image;
    }

    /**
     * @description 写入img
     * @param source
     * @param imgPath  图片地址
     * @param qrWidth
     * @param qrHeight
     * @param needCompress
     * @throws Exception
     */
    private static void insertImage(final BufferedImage source, final String imgPath, final int qrWidth, final int qrHeight, final boolean needCompress) throws Exception {
        File file = new File(imgPath);
//        if (!file.exists()) {
//            System.err.println("=================" + imgPath + "   QRCODE LOGO FILE NOT FOUND! --BY ZMQ===================");
//            return;
//        }
        Image src = ImageIO.read(new File(imgPath));
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        if (needCompress) {
            if (width > qrWidth / 3) {
                width = qrWidth / 3;
            }
            if (height > qrHeight / 4) {
                height = qrHeight / 4;
            }
            final Image image = src.getScaledInstance(width, height, 2);
            final BufferedImage tag = new BufferedImage(width, height, 1);
            final Graphics g = tag.getGraphics();
            g.drawImage(image, 0, 0, null);
            g.dispose();
            src = image;
        }
        final Graphics2D graph = source.createGraphics();
        final int x = (qrWidth - width) / 2;
        final int y = (qrHeight - height) / 2;
        graph.drawImage(src, x, y, width, height, null);
        //final Shape shape = new RoundRectangle2D.Float(x, y/4, width, width, 6.0f, 6.0f);

        graph.setStroke(new BasicStroke(3.0f));
        //graph.draw(shape);
        graph.dispose();
    }
}
